#include "registerform.h"

#include "ui_registerform.h"

#include <Application.h>
#include <QDebug>
#include <QMessageBox>
#include <QTimer>
#include <QToolTip>
#include <data/UserInfo.pb.h>
#include <method/Method.pb.h>
#include <method/Request.pb.h>
#include <method/Response.pb.h>

using namespace proto;

RegisterForm::RegisterForm(QWidget* parent) : BasicWindows(parent),
                                              ui(new Ui::RegisterForm) {
    ui->setupUi(this);

    initTitleBar();
    init();
}

RegisterForm::~RegisterForm() {
    delete ui;
}

data::Account& RegisterForm::getAccount() {
    return account;
}

void RegisterForm::init() {
    connect(ui->btn_register, &QPushButton::clicked, this, &RegisterForm::onRegisterButtonClicked);
    connect(Application::instance(), &Application::signalRegister, this, &RegisterForm::onRegisterUser);
    connect(Application::instance(), &Application::signalSendVerificationCode, this, &RegisterForm::onSendVerificationCode);
    connect(Application::instance(), &Application::signalVerifyVerificationCode, this, &RegisterForm::onVerifyVerificationCode);
}

void RegisterForm::onVerifyVerificationCode(int code, QString message) {
    if (code == 0) {
        ui->btn_register->setEnabled(true);
        ui->layoutVerificationCode->setEnabled(false);
        ui->btn_VerificationCode->setEnabled(false);
        QMessageBox::about(this, "验证码", "验证成功");
        isVerification = true;
    }
}

void RegisterForm::onSendVerificationCode(int code, QString message, QString timeout) {
    if (code == 0) {
        QMessageBox::about(this, "验证码", "验证码已发送");
        ui->btn_VerificationCode->setText("验证");
        isSendVerification = true;
    }
}

void RegisterForm::onRegisterButtonClicked() {
    qDebug() << "onRegisterButtonClicked";

    QString bind             = ui->edit_bind->text();
    QString userAccount      = ui->edit_account->text();
    QString userPassword     = ui->edit_password->text();
    QString userPasswordAgin = ui->edit_password_agin->text();
    if (bind == "") {
        showTipText("邮箱或电话号码不能为空");
        ui->edit_account->setFocus();
        return;
    }

    if (userAccount == "") {
        showTipText("用户名不能为空");
        ui->edit_account->setFocus();
        return;
    }
    if (userPassword == "") {
        showTipText("密码不能为空");
        ui->edit_password->setFocus();
        return;
    }
    if (userPasswordAgin == "") {
        showTipText("确认密码不能为空");
        ui->edit_password_agin->setFocus();
        return;
    }
    if (userPassword != userPasswordAgin) {
        showTipText("两次密码不一致");
        ui->edit_password_agin->setFocus();
        return;
    }

    data::RegisterInfo info;
    info.set_account(userAccount.toStdString());
    info.set_password(userPassword.toStdString());
    Application::instance()
        ->Register(info);
}

void RegisterForm::onRegisterUser(int code, QString message, std::shared_ptr<data::UserInfo> info) {
    if (code == 0) {
        account.set_account(info->account());
        account.set_userid(info->userid());

        QMessageBox::information(this, "通知", "注册成功,即将跳转回登录界面");
        QTimer* time = new QTimer(this);

        time->connect(time, &QTimer::timeout, [this]() { close(); });
        time->start(500);
    }
    else if (code == 1) {
    }
    else {
        ui->edit_bind->setFocus();
        QMessageBox::information(this, "通知", message);
    }
}

void RegisterForm::on_btn_VerificationCode_clicked() {
    QString bind = ui->edit_bind->text();
    if (!isSendVerification) {
        if (bind == "") {
            showTipText("邮箱或电话号码不能为空");
            ui->edit_account->setFocus();
            return;
        }
        Application::instance()->SendVerificationCode(bind.toStdString());
    }
    else {
        auto code = ui->edit_VerificationCode->text();
        if (code == "") {
            showTipText("验证码不能为空");
            ui->edit_VerificationCode->setFocus();
            return;
        }
        Application::instance()->VerifyVerificationCode(code.toStdString());
    }
}
